package sf.database.template.enjoy.ext;

import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.io.CharWriter;
import com.jfinal.template.io.FastStringWriter;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

/**
 * 必须使用#pageIgnoreTag() ... #end
 */
public class PageIgnoreTagDirective extends Directive {
    @Override
    public void exec(Env env, Scope scope, Writer writer) {
        Boolean isCount = (Boolean) scope.get(SqlKit.SQL_COUNT_KEY);
        if (isCount != null && isCount) {
            write(writer, "");
        } else {
            CharWriter charWriter = new CharWriter(64);
            FastStringWriter fsw = new FastStringWriter();
            charWriter.init(fsw);
            try {
                stat.exec(env, scope, charWriter);
            } finally {
                charWriter.close();
            }
            String content = fsw.toString();
            write(writer, content);
        }
    }

    @Override
    public boolean hasEnd() {
        return true;
    }
}
